
package ventanas;

import baseDeDatosJDBC.AdministradorDeConexiones;
import clases.TFnumericos;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.JTextField;



public class CrearAnio extends javax.swing.JInternalFrame {
    
    public static boolean abierta = false;

    public CrearAnio() {
        initComponents();
        abierta = true;
        getContentPane().setBackground(new java.awt.Color(205, 200, 177));

        //Definimos la dimension de la ventana hija.
        setPreferredSize(new java.awt.Dimension(400, 340));

        //Posicionamos la ventana interna en la esquina izquierda.
        setLocation(180, 50);
        pack();
        limitarTextField(jTextField1, 4);
    }
    
    
    Calendar fecha = new GregorianCalendar();
    int anioActual = fecha.get(Calendar.YEAR);

    public static void limitarTextField(JTextField textField, int maximo) {
        textField.setDocument(new TFnumericos(textField, maximo));
    }


    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(205, 200, 177));
        setClosable(true);
        setTitle("Crear Año");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jPanel1.setBackground(new java.awt.Color(205, 200, 177));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nuevo año", 2, 0, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel1.setText("Ingrese el año que desea crear:");

        jButton1.setBackground(new java.awt.Color(205, 200, 177));
        jButton1.setText("Crear Año");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("4 Caracteres");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(205, 200, 177));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Recordatorio", 2, 0, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel2.setText("Recuerde verificar el año de su ordenador, debe ser el actual.");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(33, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 4, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
        abierta = false;
    }//GEN-LAST:event_formInternalFrameClosed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
 
        if (jTextField1.getText().length() < 4) {

            JOptionPane.showMessageDialog(this, "El año debe tener 4 caracteres", "Advertencia", 0,
                    new javax.swing.ImageIcon(getClass().getResource("/imagenes/informacion.png")));

            jTextField1.requestFocus();
            jTextField1.selectAll();
        } else if ((Integer.parseInt(jTextField1.getText()) < 1900) || (Integer.parseInt(jTextField1.getText()) > anioActual)) {

            JOptionPane.showMessageDialog(this, "El año no puede ser mayor a " + anioActual + " ni menor a 1900", "Advertencia", 0,
                    new javax.swing.ImageIcon(getClass().getResource("/imagenes/informacion.png")));
            jTextField1.requestFocus();
            jTextField1.selectAll();
        } else {
            
            try {
                Connection conn = AdministradorDeConexiones.obtenerConexion();
                PreparedStatement preparedStatement = conn.prepareStatement(" CREATE  TABLE `siesca`.`datos" + "" + jTextField1.getText() + "` (`anio` int(10) unsigned default NULL,"
                        + "`provincia` int(10) unsigned default NULL,"
                        + "`id_departamento` int(10) unsigned NOT NULL default '0',"
                        + "`id_establecimiento` int(10) unsigned NOT NULL default '0',"
                        + "`id_servicio` int(10) unsigned NOT NULL default '0',"
                        + "`orden` int(10) unsigned NOT NULL default '0',"
                        + "`mes` int(10) unsigned NOT NULL default '0',"
                        + "`id_area` int(10) unsigned default NULL,"
                        + "`horas` int(10) unsigned default NULL,"
                        + "`hoja` int(10) unsigned default NULL,"
                        + "`C_nuevas` int(10) unsigned default NULL,"
                        + "`C_repetidas` int(10) unsigned default NULL,"
                        + "`C_mas_men_1` int(10) unsigned default NULL,"
                        + "`C_fem_men_1` int(10) unsigned default NULL,"
                        + "`C_mas_1` int(10) unsigned default NULL,"
                        + "`C_fem_1` int(10) unsigned default NULL,"
                        + "`C_mas_2a4` int(10) unsigned default NULL,"
                        + "`C_fem_2a4` int(10) unsigned default NULL,"
                        + "`C_mas_5a9` int(10) unsigned default NULL,"
                        + "`C_fem_5a9` int(10) unsigned default NULL,"
                        + "`C_mas_10a14` int(10) unsigned default NULL,"
                        + "`C_fem_10a14` int(10) unsigned default NULL,"
                        + "`C_mas_15a49` int(10) unsigned default NULL,"
                        + "`C_fem_15a49` int(10) unsigned default NULL,"
                        + "`C_mas_may_50` int(10) unsigned default NULL,"
                        + "`C_fem_may_50` int(10) unsigned default NULL,"
                        + "`C_1_vez` int(10) unsigned default NULL,"
                        + "`C_ult_vez` int(10) unsigned default NULL,"
                        + "`C_total` int(10) unsigned default NULL,"
                        + "`T_control_embarazo` int(10) unsigned default NULL,"
                        + "PRIMARY KEY  (`id_departamento`,`id_establecimiento`,`id_servicio`,`orden`,`mes`))"
                        + " ENGINE=InnoDB DEFAULT CHARSET=utf8;");
                
                preparedStatement.executeUpdate();
                preparedStatement.close();
                conn.close();
                
                JOptionPane.showMessageDialog(this, "El año ha sido creado", "Confirmación", 0,
                    new javax.swing.ImageIcon(getClass().getResource("/imagenes/informacion.png")));
                

            } catch (Exception er) {
                JOptionPane.showMessageDialog(this, "El año " + jTextField1.getText() + " ya existe!", "Advertencia", 0,
                    new javax.swing.ImageIcon(getClass().getResource("/imagenes/advertenciaDI.png")));
            }
        }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
